package com.example.springbootdemo.config;

import com.example.springbootdemo.utils.MyStringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger2Config {

    private final Logger logger = LoggerFactory.getLogger(Swagger2Config.class);

    public Swagger2Config(){
        logger.info("druid监控平台地址:http://"+ ApplicationProperties.getSwaggerUrl() +"/druid/index.html");
        logger.info("swagger2地址:http://"+ ApplicationProperties.getSwaggerUrl() +"/swagger-ui.html");
    }
    @Bean
    public Docket createRestApi() {
        List<Parameter> pars = new ArrayList<Parameter>();

        ParameterBuilder ticketPar = new ParameterBuilder();
        ticketPar.name("token").description("用户token")
                .modelRef(new ModelRef("String")).parameterType("header").defaultValue("-1")
                .required(false).build();
        pars.add(ticketPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .enable(ApplicationProperties.getSwaggerEnable())
                .groupName(ApplicationProperties.getSwaggerGroupName())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars)
                .apiInfo(apiInfo());
    }


    private ApiInfo apiInfo() {
        StringBuilder sb = new StringBuilder();
        if(ApplicationProperties.getSwaggerUrl().split(":")[1].equals("8880")){
            String[] opens = ApplicationProperties.getOpen().split(",");
            for (String open : opens) {
                String[] split = open.split(":");
                if("1".equals(split[1])){
                    String substring = MyStringUtil.substring(ApplicationProperties.getSwaggerDescription(), split[0] + ".", "." + split[0]);
                    sb.append(substring);
                }
            }
        }else{
            sb.append(ApplicationProperties.getSwaggerDescription());
        }

        return new ApiInfoBuilder()
                .title(ApplicationProperties.getSwaggerTitle())
                .description(sb.toString())
                .version(ApplicationProperties.getSwaggerVersion())
                .build();
    }
}
